//
//  UUTipsView.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 05/03/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUTipsView.h"
#import "UUResourceManager.h"
#import <Masonry/Masonry.h>

#define HEIGHT      (35)

@interface UUTipsView ()

@property (nonatomic, weak) DKBaseVC *containerViewController;
@property (nonatomic, strong) UILabel *supplierReviewStatusTipsView;

@property (nonatomic, copy) NSString *currentText;

@end

@implementation UUTipsView

- (instancetype)initWithContainerViewController:(DKBaseVC *)containerViewController {
    self = [super initWithFrame:CGRectZero];
    if (self) {
        self.containerViewController = containerViewController;
        [containerViewController.view addSubview:self];

        self.supplierReviewStatusTipsView = [UILabel new];
        self.supplierReviewStatusTipsView.font = [GetResourceManager() iconFontWithSize:12];
        self.supplierReviewStatusTipsView.numberOfLines = 2;
        self.supplierReviewStatusTipsView.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.supplierReviewStatusTipsView];
        
        [self.supplierReviewStatusTipsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(HEIGHT);
            
            if ([[self.containerViewController class] enableTranslucentNavigationBar]) {
                make.top.equalTo(self.containerViewController.translucentNavigationBar.mas_bottom);
            } else {
                make.top.equalTo(self.containerViewController.mas_topLayoutGuideBottom);
            }
        }];
        
        self.alpha = 0;
    }
    return self;
}

- (void)showTipsViewWithText:(NSString *)text error:(BOOL)error {
    if ([text isEqualToString:self.currentText]) {
        return;
    } else {
        self.currentText = text;
    }
    
    UIColor *textColor;
    UIColor *backgroundColor;
    if (!error) {
        textColor = RGB_COLOR(255, 152, 36);
        backgroundColor = RGB_COLOR(255, 242, 228);
    } else {
        textColor = [UIColor whiteColor];
        backgroundColor = COMMON_TEXT_RED_COLOR;
    }
    
    self.supplierReviewStatusTipsView.textColor = textColor;
    self.supplierReviewStatusTipsView.backgroundColor = backgroundColor;
    
    self.supplierReviewStatusTipsView.text = text;
    
    self.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, -HEIGHT);
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformIdentity;
    }];
}

- (void)hidesTipsView {
    self.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:0.25 animations:^{
        self.transform = CGAffineTransformTranslate(self.transform, 0, -HEIGHT);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        self.currentText = nil;
    }];
}

@end
